package com.yzmoney.wechat.action;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.common.model.JsonResult;
import com.yzmoney.wechat.service.WechatSendMsgService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by DOVE on 2015/8/31.
 */
@Controller
@RequestMapping("/wechat")
public class WeChatSendTpMsgAction {
	private static Logger logger = Logger
			.getLogger(WeChatSendTpMsgAction.class);

	@Resource
	private WechatSendMsgService wechatSendMsgService;

	/**
	 * 发送模版消息
	 * 
	 * @param request
	 * @param response
	 * @param  //type
	 *            请求方法的类型，暂时为公众号-gzh,企业号-qyh
	 * @param //tpMsg
	 *            请求模版的json 参数
	 * @return
	 */
	@RequestMapping("/sendMsg")
	 @ResponseBody
	 public JsonResult sendTpMsg(HttpServletRequest request,
								 HttpServletResponse response) {
		logger.info(Constants.LOGPRIFIX + "---sendMsg begin");
		String type = request.getParameter("type");
		String tpMsg = request.getParameter("tpMsg");
		JsonResult jsonResult = new JsonResult();
		logger.info(Constants.LOGPRIFIX + "type:" + type + "----tpMsg="
				+ tpMsg);
		if (StringUtils.isNotEmpty(tpMsg)) {

			if(StringUtils.containsIgnoreCase(tpMsg, "agentid")){
				type = "qyh";
			} else{
				type = "gzh";
			}
			jsonResult = wechatSendMsgService.sendTemplateMsg(tpMsg, type);
			return jsonResult;
		}
		jsonResult.setCode(Constants.CODE_FAILURE);
		jsonResult.setMsg("请传入有效的模版");
		return jsonResult;
	}

	@RequestMapping("/sendGZHMsg")
	@ResponseBody
	public JsonResult sendGZHTpMsg(HttpServletRequest request,
								HttpServletResponse response) {
		logger.info(Constants.LOGPRIFIX + "---sendMsg begin");
		String type = request.getParameter("type");
		String tpMsg = request.getParameter("tpMsg");
		JsonResult jsonResult = new JsonResult();
		logger.info(Constants.LOGPRIFIX + "type:" + type + "----tpMsg="
				+ tpMsg);
		if (StringUtils.isNotEmpty(tpMsg)) {
			jsonResult = wechatSendMsgService.sendTemplateMsg(tpMsg, "gzh");
			return jsonResult;
		}
		jsonResult.setCode(Constants.CODE_FAILURE);
		jsonResult.setMsg("请传入有效的模版");
		return jsonResult;
	}
	@RequestMapping("/sendQYHMsg")
	@ResponseBody
	public JsonResult sendGQYHTpMsg(HttpServletRequest request,
								   HttpServletResponse response) {
		logger.info(Constants.LOGPRIFIX + "---sendMsg begin");
		String type = request.getParameter("type");
		String tpMsg = request.getParameter("tpMsg");
		JsonResult jsonResult = new JsonResult();
		logger.info(Constants.LOGPRIFIX + "type:" + type + "----tpMsg="
				+ tpMsg);
		if (StringUtils.isNotEmpty(tpMsg)) {
			jsonResult = wechatSendMsgService.sendTemplateMsg(tpMsg, "qyh");
			return jsonResult;
		}
		jsonResult.setCode(Constants.CODE_FAILURE);
		jsonResult.setMsg("请传入有效的模版");
		return jsonResult;
	}
}
